package com.dongdong.controller;


import com.dongdong.aop.LogAn;
import com.dongdong.model.TbSpecification;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.result.Result;
import com.dongdong.result.ResultConstant;
import com.dongdong.service.SpecService;


import com.dongdong.vo.SpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@Controller
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;


    @GetMapping("findAll")
        public Result findAll(){
     List<SpecificationVO> specificationVOS= specService.findAll();
        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG,specificationVOS); //4：操作成功

    }
    //分页查询所有品牌信息带模糊查询
    @LogAn  //自定义前置通知和返回通知
    @PostMapping("/findAllPage")
    public Result findAllPage(@RequestBody PageBean<SpecificationVO> pageBean){

//        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG,result); //4：操作成功
        try {
            PageResult<SpecificationVO> result = specService.findAllPage(pageBean);
            return Result.OK().setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }

    }


    @PostMapping("add")
    public Result add(@RequestBody SpecificationVO specVO){
         specService.insert(specVO);
        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG); //4：操作成功
    }
//根据id查询规格和规格组成
    @GetMapping("selectbyid")
    public Result selectbyid(@RequestParam Long id){
        SpecificationVO specificationVO= specService.selectbyid(id);
        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG,specificationVO); //4：操作成功
    }
//修改
    @PostMapping("update")
    public Result update(@RequestBody SpecificationVO specVO){
        specService.update(specVO);
        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG); //4：操作成功
    }

    @GetMapping("deletebyid")
    public Result deletebyid(@RequestParam Long id){
         specService.deletebyid(id);
        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG); //4：操作成功
    }

    //批量删除
    @GetMapping("deleteCheckids")
    public Result  deleteCheckids(@RequestParam Integer[] ids) {
        int i = specService.deletebyids(ids);
        System.out.println(i);
        if (i <1) {
            return new Result(ResultConstant.ERROR_CODE, ResultConstant.ERROR_MSG); //5:失败

        }
        return new Result(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG); //4：操作成功

    }

}
